package com.swind.fast.modules.app.railway.project.service.impl;

import com.swind.fast.common.system.base.service.impl.SwindServiceImpl;
import com.swind.fast.modules.app.railway.project.vo.RailwayProjectWorkmanVo;
import com.swind.fast.modules.app.railway.project.entity.RailwayProjectWorkman;
import com.swind.fast.modules.app.railway.project.mapper.RailwayProjectWorkmanMapper;
import com.swind.fast.modules.app.railway.project.service.IRailwayProjectWorkmanService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.swind.fast.modules.system.entity.SysUser;
import com.swind.fast.modules.system.service.ISysUserService;
import com.swind.fast.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Optional;

/**
 * @Description: 项目
 */
@Slf4j
@Service
public class RailwayProjectWorkmanServiceImpl extends SwindServiceImpl<RailwayProjectWorkmanMapper, RailwayProjectWorkman> implements IRailwayProjectWorkmanService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public IPage<?> getWorkmanListPage(IPage<RailwayProjectWorkman> page, QueryWrapper<RailwayProjectWorkman> queryWrapper) {

        IPage<RailwayProjectWorkman> pageList = this.page(page, queryWrapper);
        if (pageList.getRecords().size() < 1) return pageList;

        // 获得负责人ID
        List<String> listIds = pageList.getRecords().stream().map(RailwayProjectWorkman::getUserId).distinct().collect(Collectors.toList());
        if (listIds.size() < 1) return pageList;

        QueryWrapper<SysUser> markQueryWrapper = new QueryWrapper<SysUser>().select("id", "realname", "phone").in("id", listIds);
        List<SysUser> listUsers = sysUserService.list(markQueryWrapper);
        // log.info("listIds: {}, listUsers: {}", listIds, listUsers);

        IPage<RailwayProjectWorkmanVo> rpageList = pageList.convert(item -> {
            RailwayProjectWorkmanVo resp = oConvertUtils.entityToModel(item, RailwayProjectWorkmanVo.class);
            // 过滤找出第一个
            Optional<SysUser> userOpt = listUsers.stream().filter(p -> p.getId().equals(item.getUserId())).findFirst();

            if (userOpt.isPresent()) {
                SysUser user = userOpt.get();
                resp.setUserName(user.getRealname());
                resp.setUserPhone(user.getPhone());
            }

            return resp;
        });

        return rpageList;
    }

    public String getWorkmanByPosition(String projectId, String position) {
        QueryWrapper<RailwayProjectWorkman> queryWrapper = new QueryWrapper<RailwayProjectWorkman>().select("id", "user_id").eq("project_id", projectId).eq("position", position).last("limit 1");
        RailwayProjectWorkman man = this.getOne(queryWrapper);
        if (oConvertUtils.isEmpty(man)) return "";

        String userId = man.getUserId();
        SysUser user = sysUserService.getById(userId);
        if (oConvertUtils.isEmpty(user)) return "";
        return user.getRealname();
    }

    @Override
    public List<String> getProjectIdsByUserId(String userId) {
        List<String> retList = null;
        QueryWrapper<RailwayProjectWorkman> queryWrapper = new QueryWrapper<RailwayProjectWorkman>().select("DISTINCT project_id").eq("user_id", userId);
        List<RailwayProjectWorkman> list = this.list(queryWrapper);
        if (list.size() > 0) {
            retList = list.stream().map(RailwayProjectWorkman::getProjectId).collect(Collectors.toList());
        }
        return retList;
    }

}
